﻿using System.Collections.Generic;
using System.Linq;


using BLToolkit.DataAccess;
using BLToolkit.Data.Linq;
using BLToolkit.Mapping;
namespace Core
{
    [TableName("tblBangCap")]
    public class BangCap
    {

        [PrimaryKey, Identity]
        [MapField("MaBangCap")]
        public int ID { get; set; }
        public string TenBangCap { get; set; }
        [Association(ThisKey = "MaBangCap", OtherKey = "MaBangCap", CanBeNull = true)]
        public List<CanBo> CanBos { get; set; }

        public static IQueryable<BangCap> BangCaps
        {
            get
            {
                using (var db = new DataContext())
                {
                    return db.GetTable<BangCap>();
                }
            }
        }
        public static List<BangCap> All()
        {
            return BangCaps.ToList();
        }
        public static BangCap Single(int id)
        {
            return BangCaps.SingleOrDefault(x => x.ID == id);
        }
        public static int Add(BangCap entity)
        {
            using (var db = new DataContext())
            {
                object rs = db.InsertWithIdentity(entity);
                return int.Parse(rs.ToString());
            }
        }
        public static int Update(BangCap entity)
        {
            using (var db = new DataContext())
            {
                return db.Update(entity);
            }
        }
        public static int Delete(int id)
        {
            return BangCaps.Delete(x => x.ID == id);
        }
    }
}
